//require('../css/newroom.css');
(function(rf){
     var opt = {
         getDoms : function(){
             this.doms = {
                 create : $('.create'),
                 createroom : $('.createroom'),
                 Return : $('.Return'),
                 chekbox : $('.chekbox'),
                 Password : $('.Password'),
                 getpassword : $('.getpassword'),
                 getNumber : $('.getNumber'),
                 showNumber : $('.showNumber'),
                 sum : $('.sum').html(),
                 showPassword : $('.showPassword'),
                 createbtn : $('.createbtn'),
                 createroom : $('.createroom')
             }
         },
         init : function(){
             var _self = this;
             _self.getDoms();
             _self.addEvent();
         },
         addEvent : function(){
            var _self = this;
             _self.doms.createbtn.on('click',function(){
                 var url = _self.doms.createroom.attr('data');
                 var chekboxNmuber = 0;
                 for(var i=0;i<_self.doms.chekbox.length;i++){
                    if($(_self.doms.chekbox[i]).attr('bolen')=='true'){
                        chekboxNmuber = $(_self.doms.chekbox[i]).attr('number')
                    }
                 }
                 var mid = window.location.href.split('?')[1].split('&')[0];
                 var token = window.location.href.split('?')[1].split('&')[1];
                 var opt = {
                     url : url,
                     data : 'round='+chekboxNmuber+'&mid='+mid.split('=')[1]+'&token='+token.split('=')[1]
                     };
                 rf.getData(opt,function(data){
                     if(data.status){
                         window.location.href =  window.location.href.replace('NewRoom','waitfor')+'&role='+data.role+'&rid='+data.rid;
                     }
                 })
             });
             _self.doms.create.find('li').on('click',function(){
                    var index = $(this).index();
                    if(index==0){
                      if(parseInt(_self.doms.sum)<=0){
                          alert('房卡余额不足')
                      }else {
                          _self.doms.createroom.css('display', 'block')
                      }
                    }else{
                        _self.doms.Password.css('display','block');
                    }
             });
             _self.doms.Return.on('click',function(){
                 _self.doms.createroom.css('display','none');
                 _self.doms.Password.css('display','none')
             });
             _self.doms.chekbox.on('click',function(){
                 var src = $(this).attr('src');
                 _self.doms.chekbox.attr('bolen','false');
                 $(this).attr('bolen','true');
                 _self.doms.chekbox.attr('src',"/home/imgs/cb0.png");
                if(src.indexOf('1')!=-1){
                    src = src.replace('1','0');
                    $(this).attr('src',src)
                }else{
                    src = src.replace('0','1');
                    $(this).attr('src',src)
                }
             });
             _self.doms.getNumber.on('click',function(){
                 _self.setPassword(parseInt($(this).parent().attr('data')),_self,function(){
                    var url =  'http://mj.lcc.so/game/join';
                    var mid = window.location.href.split('?')[1];
                    var passwordNumber = '';
                     for(var i=0;i<_self.doms.showPassword.find('li').length;i++){
                         passwordNumber +=parseInt(_self.doms.showPassword.find('li').eq(i).find('img').attr('src').split('_')[1])
                     }
                       var opt = {
                           url : url,
                           data : 'pwd='+passwordNumber+'&'+mid
                       };
                     window.getData(opt,function(data){
                         alert(data.msg);
                         if(data.status){
                             // var wsServer ='ws://192.168.0.199:9502';  //地址
                             var wsServer ='ws://139.224.68.110:9502';  //地址
                             var ws = new WebSocket(wsServer); //监听链接
                             ws.onopen = function (evt) {  //测试链接成功或失败
                                 //websocket.readyState 属性：
                                 switch (ws.readyState) {
                                     case 0: console.log('通信服务未打开');break;
                                     case 1: console.log('连接成功');break;
                                     case 2: console.log('通信端口关闭，无法连接');break;
                                     case 3: console.log('通信服务已关闭');break;
                                 }
                                 var json = {
                                    type : 0
                                 };
                                 ws.send(json)
                             };
                             window.location.href = window.location.href.replace('NewRoom','waitfor')+'&role='+data.role+'&rid='+data.rid;
                         }
                     })
                 })
             })
         },
         setPassword : function(index,_self,callback){

                if(index!=10&&index!=11){
                    for(var i=0;i<_self.doms.showNumber.length;i++ ){
                        if(_self.doms.showNumber.eq(i).css('display')=='none'){
                            var src = _self.doms.showNumber.eq(i).find('img').attr('src');
                            src = src.replace('0',index);
                            _self.doms.showNumber.eq(i).find('img').attr('src',src);
                                _self.doms.showNumber.eq(i).css('display','block');
                            if(i==5){
                                callback();
                            }
                            return
                        }
                    }
                }else if(index==10){
                    for(var i=0;i<_self.doms.showNumber.length;i++ ){
                        if(_self.doms.showNumber.eq(i).css('display')=='none'){
                                if(i>=1){
                                    _self.doms.showNumber.eq(i-1).find('img').attr('src','../imgs/btn_0.png').
                                        parent().css('display','none');
                                }
                        }
                    }
                }else{
                    for(var i=0;i<_self.doms.showNumber.length;i++ ){
                                _self.doms.showNumber.eq(i).find('img').attr('src','../imgs/btn_0.png').
                                    parent().css('display','none');

                        }
                }
         }

     };
    opt.init();
})(window);